package ret

import (
	"mygame/app/common"
	"net/http"

	"github.com/gin-gonic/gin"
)

func Success(ctx *gin.Context, data any) {
	ctx.JSON(http.StatusOK, gin.H{
		"code": common.CODE_SUCCESS,
		"data": data,
		"msg":  common.CodeMessage[common.CODE_SUCCESS],
	})
}

func Error(ctx *gin.Context, msg string) {
	if msg == "" {
		msg = common.CodeMessage[common.CODE_FAIL]
	}
	ctx.JSON(http.StatusBadRequest, gin.H{
		"code": common.CODE_FAIL,
		"msg":  msg,
	})
	ctx.Abort()
}

func ErrorWithCode(ctx *gin.Context, code int, msg string) {
	if msg == "" {
		msg = common.CodeMessage[code]
	}
	ctx.JSON(http.StatusBadRequest, gin.H{
		"code": code,
		"msg":  msg,
	})
	ctx.Abort()
}
